package com.edfeff.nio.http;

import com.edfeff.nio.ReactorSimpleHttpServer;

import java.io.IOException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class Acceptor implements Runnable {
    private final ReactorSimpleHttpServer server;
    private final ServerSocketChannel socketChannel;

    public Acceptor(ReactorSimpleHttpServer server) {
        this.server = server;
        socketChannel = server.getSocketChannel();
    }

    @Override
    public void run() {
        while (server.isRun()) {
            try {
                SocketChannel socket = socketChannel.accept();
                if (socket != null)
                    server.processSocket(socket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
